layui.config({
	base : "js/"
}).use(['form','layer','jquery','layedit','laydate','upload'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		layedit = layui.layedit,
		laydate = layui.laydate,
		$ = layui.jquery;

	//创建一个编辑器
	
	
 	var editIndex = layedit.build('news_content');
 	var addNewsArray = [],addNews;
 	var orgId = window.sessionStorage.getItem("organizeId");
 	var notId = window.sessionStorage.getItem("noticeEditId");
 	
	 	layui.upload({
    	elem: '.memLoad', //绑定元素
    	url : $.httpRootPath() + "/api/web/notice/i/"+orgId+"/"+notId+"/picture",
    	type: 'images',
		success: function(res){
         	if(res.code != "00"){
         		layer.msg("服务器异常，请稍候再试");
         	}else{
         		layer.msg("修改成功");
         		$("#orgLogo").attr("src",res.data.picture);
         	}
  		},
         error: function(){
         	 console.log("失败"); 
      	//请求异常回调
    	}
    });
 	$.ajax({
		url: $.httpRootPath() + "/api/web/notice/i/" + orgId + "/" + notId,
		type: "get",
		dataType: "json",
		success: function(data) {
			if(data.code == "00") {
				fillData(data.data);
			} else {
				layer.alert('加载失败', {
					icon: 6,
					title: '友情提示'
				});
			}

		}
	})
 	
 	function fillData(data){
 		$(".title").val(data.title);
       	$("#news_content").val(data.content);
       	$(".subContent").val(data.subContent);
		$(".state option[value='"+data.state+"']").attr("selected","selected");  
		$(".type option").each(function(){  
        	if($(this).text() == data.type){  
            	$(this).attr("selected","selected");  
        	}  
    	});  
//     $("[name='userStatus']").find("option[value="+proId+"]").attr("selected",true);  
   		form.render('select');
   		
       	if(data.picture != ""){
			$("#orgLogo").attr("src",data.picture);
		}
 	}
 	
 	form.on("submit(subInfo)",function(data){
 		layedit.sync(editIndex);
 		//弹出loading
 		var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
   		var data = {
        	title: $(".title").val(),
       		content: $("#news_content").val(),
       		type:$(".type option").eq($(".type").val()).text(),
       		subContent:$(".subContent").val(),
       		state:$('.state option:selected') .val(),//选中的值
       		orgId:orgId,
       		notId:notId
   		 };
 		updateInfo(data,function(){
 			top.layer.close(index);
 			layer.closeAll("iframe");
	 		//刷新父页面
//	 		parent.location.reload();
 		})
 		return false;
 	})
})

function updateInfo(data,callback) {
    $.ajax({
        url: $.httpRootPath()+'/api/web/notice/i/'+data.orgId+"/"+data.notId,
        type: 'PUT',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            if (r.code == "00") {
              	layer.msg("更新成功！");
            } else {
               layer.msg("更新失败！");
            }
        },
        error: function (e) {
          layer.msg("系统错误！");
        },
        complete:function(){
        	callback();	
        }
    });

}
